package cn.com.panda;

import static android.webkit.WebSettings.LOAD_NO_CACHE;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.webkit.WebView;

import org.json.JSONObject;

import cn.com.hesc.library.ActionListener;
import cn.com.hesc.library.ActionType;
import cn.com.hesc.library.ExtendsWebView;
import cn.com.hesc.library.JavaScriptInterface;
import cn.com.hesc.library.MessageHandler;
import cn.com.hesc.library.entity.StorageBean;
import cn.com.panda.base.BaseActivity;
import cn.com.panda.utils.SharedPreferencesUtil;

/**
 * 用户隐私政策等常规页面
 */
public class WebViewActivity extends BaseActivity implements ActionListener {

    private  ExtendsWebView webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);

        webview = findViewById(R.id.href);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setCacheMode(LOAD_NO_CACHE);
        webview.loadUrl(getIntent().getExtras().getString("url"));
        webview.addJavascriptInterface(new JavaScriptInterface(this),"Native");
    }

    @Override
    protected void checkPermissionResult(String[] permissions, int[] grantResults) {

    }

    @Override
    public void onAction(ActionType actionType, String params) {
        try {
            JSONObject jsonObject = new JSONObject(params);
            MessageHandler messageHandler = new MessageHandler();
            messageHandler.setUniqueId(jsonObject.getString("callbackId"));
            switch (actionType){
                case DEVICE_GETDATA:
                    getPrivateData(messageHandler,params);
                    break;
                case DEVICE_SAVEDATA:
                    setPrivateData(messageHandler,params);
                    break;
                case DEVICE_REMOVEDATA:
                    removePrivateData(messageHandler,params);
                    break;
                case DEVICE_OPENWEB:
                    openActivity(messageHandler,params);
                    break;
                case NAVIGATION_BAR_CLOSE:
                    WebViewActivity.this.finish();
                    break;
                case NATIVE_METHOD:
                    if("exit".equals(jsonObject.getString("exit"))){
                        System.exit(0);
                    }else if("logout".equals(jsonObject.getString("exit"))){
                        toActivity(MainActivity.class,null);
                        WebViewActivity.this.finish();
                    }
                    break;
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void getPrivateData(MessageHandler messageHandler, String params) {
        SharedPreferencesUtil sharedPreferencesUtil = new SharedPreferencesUtil(mContext);
        try {
            JSONObject jsonObject = new JSONObject(params);
            String param = jsonObject.getString("name");
            String value = sharedPreferencesUtil.getString(param);
            StorageBean storageBean = new StorageBean();
            storageBean.setResult(true);
            storageBean.setValue(value);
            messageHandler.setSuccess(true);
            messageHandler.setRes(storageBean);
            webview.excuteJs(messageHandler);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void removePrivateData(MessageHandler messageHandler, String params) {
        SharedPreferencesUtil sharedPreferencesUtil = new SharedPreferencesUtil(mContext);
        try {
            JSONObject jsonObject = new JSONObject(params);
            if(jsonObject.has("name")){
                sharedPreferencesUtil.saveString(jsonObject.getString("name"),"");
                webview.excuteJs(messageHandler);
            }else{
                String error = "缺乏key";
                messageHandler.setRes(error);
                messageHandler.setSuccess(false);
                webview.excuteJs(messageHandler);
            }
        }catch (Exception e){
            e.printStackTrace();
            String error = "数据格式错误";
            messageHandler.setRes(error);
            messageHandler.setSuccess(false);
            webview.excuteJs(messageHandler);
        }
    }

    private void setPrivateData(MessageHandler messageHandler, String params) {
        SharedPreferencesUtil sharedPreferencesUtil = new SharedPreferencesUtil(mContext);
        try {
            JSONObject jsonObject = new JSONObject(params);
            if(jsonObject.has("name") && jsonObject.has("value")){
                sharedPreferencesUtil.saveString(jsonObject.getString("name"),jsonObject.getString("value"));
                webview.excuteJs(messageHandler);
            }else{
                String error = "缺乏key或者value";
                messageHandler.setRes(error);
                messageHandler.setSuccess(false);
                webview.excuteJs(messageHandler);
            }
        }catch (Exception e){
            e.printStackTrace();
            String error = "数据格式错误";
            messageHandler.setRes(error);
            messageHandler.setSuccess(false);
            webview.excuteJs(messageHandler);
        }
    }

    private void openActivity(MessageHandler messageHandler, String params) {
        try{
            JSONObject jsonObject = new JSONObject(params);
            String urlstr = jsonObject.optString("url","");
            //打开蓝牙界面
            if("bluetooth".equals(urlstr)){
                Bundle bundle = new Bundle();
                bundle.putString("btdevice","BseeraA001");
                toActivity(BlueToothActivity.class,bundle);
            }else if(urlstr.contains("http") || urlstr.contains("https")){
                //页面跳转href
                Bundle bundle = new Bundle();
                bundle.putString("url",urlstr);
                toActivity(WebViewActivity.class,bundle);
            }
            webview.excuteJs(messageHandler);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}